const SkipList = require("../template")

function testSkipListAdvancedOperations() {
  console.log(
    "1. 高级插入测试:",
    (() => {
      const sl = new SkipList()
      
      // 插入大量数据测试跳表结构
      const values = [50, 30, 70, 20, 40, 60, 80, 10, 25, 35, 45, 55, 65, 75, 85]
      values.forEach(val => sl.insert(val))
      
      // 验证所有值都存在
      const allFound = values.every(val => sl.search(val) === true)
      
      return allFound &&
             sl.getSize() === values.length &&
             sl.getLevel() >= 0
    })()
  )

  console.log(
    "2. 高级删除测试:",
    (() => {
      const sl = new SkipList()
      
      // 插入数据
      const values = [50, 30, 70, 20, 40, 60, 80, 10, 25, 35, 45]
      values.forEach(val => sl.insert(val))
      
      // 删除一些节点
      const toDelete = [30, 70, 10, 25]
      toDelete.forEach(val => sl.delete(val))
      
      // 验证删除结果
      const deletedCorrect = toDelete.every(val => sl.search(val) === false)
      const remainingCorrect = values.filter(val => !toDelete.includes(val))
        .every(val => sl.search(val) === true)
      
      return deletedCorrect &&
             remainingCorrect &&
             sl.getSize() === values.length - toDelete.length
    })()
  )

  console.log(
    "3. 重复值处理测试:",
    (() => {
      const sl = new SkipList()
      
      // 插入重复值
      sl.insert(5)
      sl.insert(5)
      sl.insert(5)
      
      // 验证重复值处理
      const size = sl.getSize()
      const found = sl.search(5)
      
      return size === 1 && // 重复值应该只存储一次
             found === true
    })()
  )

  console.log(
    "4. 跳表层数动态调整测试:",
    (() => {
      const sl = new SkipList()
      
      // 插入数据观察层数变化
      const initialLevel = sl.getLevel()
      
      sl.insert(10)
      const level1 = sl.getLevel()
      
      sl.insert(20)
      sl.insert(30)
      sl.insert(40)
      const level2 = sl.getLevel()
      
      return initialLevel >= 0 &&
             level1 >= initialLevel &&
             level2 >= level1
    })()
  )

  console.log(
    "5. 跳表结构验证测试:",
    (() => {
      const sl = new SkipList()
      
      // 插入数据
      const values = [10, 20, 30, 40, 50]
      values.forEach(val => sl.insert(val))
      
      // 验证跳表结构
      const allValues = sl.getAllValues()
      const min = sl.getMin()
      const max = sl.getMax()
      const size = sl.getSize()
      
      return allValues.length === 5 &&
             min === 10 &&
             max === 50 &&
             size === 5
    })()
  )

  console.log(
    "6. 边界值处理测试:",
    (() => {
      const sl = new SkipList()
      
      // 测试边界值
      const boundaryValues = [
        Number.MAX_SAFE_INTEGER,
        Number.MIN_SAFE_INTEGER,
        0,
        -1,
        1
      ]
      
      boundaryValues.forEach(val => sl.insert(val))
      
      // 验证所有边界值都存在
      const allFound = boundaryValues.every(val => sl.search(val) === true)
      
      return allFound &&
             sl.getSize() === boundaryValues.length
    })()
  )

  console.log(
    "7. 跳表清空测试:",
    (() => {
      const sl = new SkipList()
      
      // 插入数据
      sl.insert(10)
      sl.insert(20)
      sl.insert(30)
      
      const sizeBefore = sl.getSize()
      
      // 清空跳表
      sl.clear()
      
      const sizeAfter = sl.getSize()
      const isEmpty = sl.isEmpty()
      const found = sl.search(10)
      
      return sizeBefore === 3 &&
             sizeAfter === 0 &&
             isEmpty === true &&
             found === false
    })()
  )

  console.log(
    "8. 跳表重建测试:",
    (() => {
      const sl = new SkipList()
      
      // 插入数据
      const values = [10, 20, 30, 40, 50]
      values.forEach(val => sl.insert(val))
      
      // 删除所有数据
      values.forEach(val => sl.delete(val))
      
      // 重新插入
      values.forEach(val => sl.insert(val))
      
      // 验证重建结果
      const allFound = values.every(val => sl.search(val) === true)
      
      return allFound &&
             sl.getSize() === values.length
    })()
  )

  console.log(
    "9. this上下文高级操作测试:",
    (() => {
      const advancedObj = {
        multiplier: 2,
        processAdvanced: function(values) {
          const sl = new SkipList()
          values.forEach(val => sl.insert(val))
          return sl.getSize() * this.multiplier
        }
      }
      
      const values = [10, 20, 30, 40, 50]
      return advancedObj.processAdvanced(values) === 10 // 5*2=10
    })()
  )

  console.log(
    "10. 复杂对象this绑定高级操作测试:",
    (() => {
      const advancedConfigObj = {
        config: { threshold: 3 },
        processWithConfig: function(values) {
          const sl = new SkipList()
          values.forEach(val => sl.insert(val))
          return sl.getSize() >= this.config.threshold
        }
      }
      
      const values = [10, 20, 30]
      return advancedConfigObj.processWithConfig(values) === true
    })()
  )

  console.log(
    "11. 跳表一致性测试:",
    (() => {
      const sl1 = new SkipList()
      const sl2 = new SkipList()
      
      const values = [50, 30, 70, 20, 40, 60, 80]
      
      // 相同的数据插入顺序
      values.forEach(val => {
        sl1.insert(val)
        sl2.insert(val)
      })
      
      // 验证结果一致
      const allValues1 = sl1.getAllValues()
      const allValues2 = sl2.getAllValues()
      const size1 = sl1.getSize()
      const size2 = sl2.getSize()
      
      return JSON.stringify(allValues1.sort()) === JSON.stringify(allValues2.sort()) &&
             size1 === size2 &&
             size1 === values.length
    })()
  )

  console.log(
    "12. 跳表复杂操作测试:",
    (() => {
      const sl = new SkipList()
      
      // 复杂操作序列
      const operations = [
        () => sl.insert(10),
        () => sl.insert(20),
        () => sl.insert(30),
        () => sl.delete(20),
        () => sl.insert(25),
        () => sl.insert(35),
        () => sl.delete(10),
        () => sl.insert(15)
      ]
      
      operations.forEach(op => op())
      
      // 验证最终状态
      const finalValues = sl.getAllValues()
      const size = sl.getSize()
      const min = sl.getMin()
      const max = sl.getMax()
      
      return size === 4 &&
             min === 15 &&
             max === 35 &&
             finalValues.includes(15) &&
             finalValues.includes(25) &&
             finalValues.includes(30) &&
             finalValues.includes(35)
    })()
  )
}

testSkipListAdvancedOperations()
